package com.hengda.museumonline.ui;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.webkit.WebView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.hengda.museumonline.MyApplication;
import com.hengda.museumonline.R;
import com.hengda.museumonline.entity.ArchBean;
import com.hengda.museumonline.http.CallServer;
import com.litesuits.common.utils.PackageUtil;
import com.yolanda.nohttp.NoHttp;
import com.yolanda.nohttp.RequestMethod;
import com.yolanda.nohttp.rest.OnResponseListener;
import com.yolanda.nohttp.rest.Request;
import com.yolanda.nohttp.rest.Response;

/**
 * @author：JFWU on 2016/10/18 20:05
 * @E-mail：jeffy12138@126.com
 */
public class ArchContentActivity extends BaseActivity {
    private int id;
    private ArchBean archBean;
    static final String mimeType = "text/html";
    static final String encoding = "utf-8";
    private TextView title;
    private TextView date;
    private TextView source;
    private WebView content;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_arch);
        this.content = (WebView) findViewById(R.id.content);
        this.source = (TextView) findViewById(R.id.source);
        this.date = (TextView) findViewById(R.id.date);
        this.title = (TextView) findViewById(R.id.title);
        content.setBackgroundColor(0);
        content.getBackground().setAlpha(0);
        id = Integer.parseInt(getIntent().getStringExtra("id"));
        getContentfromNet();
        System.out.println("id:" + id);
    }

    private void getContentfromNet() {
        Request request = NoHttp.createStringRequest(MyApplication.url + "archaeol/content", RequestMethod.GET);
        request.add("p", "a");
        request.add("v", PackageUtil.getAppPackageInfo(this).versionName);
        request.add("id", id);

        CallServer.getRequestInstance().add(this, 0, request, new OnResponseListener() {
            @Override
            public void onStart(int what) {
            }

            @Override
            public void onSucceed(int what, Response response) {
                System.out.println("response:" + response.get().toString());
                if (200 == response.getHeaders().getResponseCode()) {
                    Gson gson = new Gson();
                    archBean = gson.fromJson(response.get().toString(), ArchBean.class);
                    if (archBean != null && archBean.getData() != null) {
                        setData(archBean);
                    }
                }
            }

            @Override
            public void onFailed(int what, String url, Object tag, Exception exception, int responseCode, long networkMillis) {
                System.out.println("onFailed:" + exception);
            }

            @Override
            public void onFinish(int what) {
            }
        });
    }

    private void setData(ArchBean archBean) {
        title.setText(archBean.getData().getTitle());
        date.setText(archBean.getData().getDate());
        source.setText(archBean.getData().getSource());
        content.loadDataWithBaseURL(null, archBean.getData().getContent(), mimeType, encoding, null);

    }
}
